﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Reflection;
using System.Web.Mvc;

namespace BOSS.Web.Extensions
{
    /// <summary>
    /// ExtJs 验证扩展
    /// </summary>
    public static class ValidationExtensions
    {
        /// <summary>
        /// ExtJs Ext.form.* 支持 markInvalid 控件的验证信息
        /// </summary>
        /// <param name="htmlHelper"></param>
        /// <returns></returns>
        public static string ExtValidationMessage(this HtmlHelper htmlHelper)
        {
            StringBuilder strBuilder = new StringBuilder();
            ModelStateDictionary modelStateDictionary = htmlHelper.ViewData.ModelState;
            if (!modelStateDictionary.IsValid)
            {
                IEnumerator<KeyValuePair<string, ModelState>> enumerator = modelStateDictionary.GetEnumerator();
                while (enumerator.MoveNext())
                {
                    string key = enumerator.Current.Key;
                    ModelState modelState = enumerator.Current.Value;
                    if (modelState.Errors != null && modelState.Errors.Count > 0)
                    {
                        strBuilder.Append(string.Format("{0}.markInvalid('{1}');", key, modelState.Errors.FirstOrDefault().ErrorMessage));
                        strBuilder.AppendLine();
                    }
                }
            }
            return strBuilder.ToString();
        }

        /// <summary>
        /// ExtJs Ext.form.* 支持 markInvalid 控件的验证信息
        /// </summary>
        /// <param name="htmlHelper"></param>
        /// <param name="fields"></param>
        /// <returns></returns>
        public static string ExtValidationMessage(this HtmlHelper htmlHelper, IDictionary<string, IList<string>> fields)
        {
            StringBuilder strBuilder = new StringBuilder();
            ModelStateDictionary modelStateDictionary = htmlHelper.ViewData.ModelState;
            if (!modelStateDictionary.IsValid)
            {
                IEnumerator<KeyValuePair<string, ModelState>> enumerator = modelStateDictionary.GetEnumerator();
                while (enumerator.MoveNext())
                {
                    string key = enumerator.Current.Key;
                    if (fields.ContainsKey(key))
                    {
                        ModelState modelState = enumerator.Current.Value;
                        if (modelState.Errors != null && modelState.Errors.Count > 0)
                        {
                            foreach (var item in fields[key])
                            {
                                strBuilder.Append(string.Format("{0}.markInvalid('{1}');", item, modelState.Errors.FirstOrDefault().ErrorMessage));
                                strBuilder.AppendLine();
                            }
                        }
                    }
                }
            }
            return strBuilder.ToString();
        }

        /// <summary>
        /// ExtJs Ext.form.* 支持 markInvalid 控件的验证信息
        /// </summary>
        /// <param name="htmlHelper"></param>
        /// <param name="fields"></param>
        /// <returns></returns>
        public static string ExtValidationMessage(this HtmlHelper htmlHelper, IDictionary<string, string> fields)
        {
            StringBuilder strBuilder = new StringBuilder();
            ModelStateDictionary modelStateDictionary = htmlHelper.ViewData.ModelState;
            if (!modelStateDictionary.IsValid)
            {
                IEnumerator<KeyValuePair<string, ModelState>> enumerator = modelStateDictionary.GetEnumerator();
                while (enumerator.MoveNext())
                {
                    string key = enumerator.Current.Key;
                    if (fields.ContainsKey(key))
                    {
                        ModelState modelState = enumerator.Current.Value;
                        if (modelState.Errors != null && modelState.Errors.Count > 0)
                        {
                            strBuilder.Append(string.Format("{0}.markInvalid('{1}');", fields[key], modelState.Errors.FirstOrDefault().ErrorMessage));
                            strBuilder.AppendLine();
                        }
                    }
                }
            }
            return strBuilder.ToString();
        }

        /// <summary>
        /// 设置 Ext.form.* 值
        /// </summary>
        /// <param name="htmlHelper"></param>
        /// <param name="fields"></param>
        /// <returns></returns>
        public static string ExtSetValues(this HtmlHelper htmlHelper, IDictionary<string, object> fields)
        {
            StringBuilder strBuilder = new StringBuilder();

            foreach (var item in fields)
            {
                if (item.Value != null && !string.IsNullOrEmpty(item.Value.ToString()))
                {
                    if (item.Value.GetType().IsEnum)
                    {
                        Enum value = (Enum)item.Value;
                        strBuilder.AppendLine(string.Format("{0}.setValue('{1}');", item.Key, value.GetDescription()));
                    }
                    else if (item.Value.GetType() == typeof(DateTime))
                    {
                        DateTime dt = Convert.ToDateTime(item.Value);
                        strBuilder.AppendLine(string.Format("{0}.setValue({1});", item.Key, string.Format("new Date({0}, {1}, {2}, {3}, {4}, {5})", dt.Year, dt.Month - 1, dt.Day, dt.Hour, dt.Minute, dt.Second)));
                    }
                    else if (item.Value.GetType() == typeof(string))
                    {
                        strBuilder.AppendLine(string.Format("{0}.setValue('{1}');", item.Key, item.Value.ToString().Replace("\n", "\\n").Replace("\r", "\\r")));
                    }
                    else
                    {
                        strBuilder.AppendLine(string.Format("{0}.setValue('{1}');", item.Key, item.Value));
                    }
                }
            }

            return strBuilder.ToString();
        }

        //setRawValue
        /// <summary>
        /// 设置 Ext.form.* 值
        /// </summary>
        /// <param name="htmlHelper"></param>
        /// <param name="fields"></param>
        /// <returns></returns>
        public static string ExtSetRawValues(this HtmlHelper htmlHelper, IDictionary<string, object> fields)
        {
            StringBuilder strBuilder = new StringBuilder();

            foreach (var item in fields)
            {
                if (item.Value != null && !string.IsNullOrEmpty(item.Value.ToString()))
                {
                    if (item.Value.GetType().IsEnum)
                    {
                        Enum value = (Enum)item.Value;
                        strBuilder.AppendLine(string.Format("{0}.setRawValue('{1}');", item.Key, value.GetDescription()));
                    }
                    else if (item.Value.GetType() == typeof(DateTime))
                    {
                        DateTime dt = Convert.ToDateTime(item.Value);
                        strBuilder.AppendLine(string.Format("{0}.setRawValue({1});", item.Key, string.Format("new Date({0}, {1}, {2}, {3}, {4}, {5})", dt.Year, dt.Month - 1, dt.Day, dt.Hour, dt.Minute, dt.Second)));
                    }
                    else if (item.Value.GetType() == typeof(string))
                    {
                        strBuilder.AppendLine(string.Format("{0}.setRawValue('{1}');", item.Key, item.Value.ToString().Replace("\n", "\\n").Replace("\r", "\\r")));
                    }
                    else
                    {
                        strBuilder.AppendLine(string.Format("{0}.setRawValue('{1}');", item.Key, item.Value));
                    }
                }
            }

            return strBuilder.ToString();
        }
    }   
}
